package com.hk.web.controller.api.frontend;

import com.hk.commons.JsonResult;
import com.hk.commons.ProcessAPI;
import com.hk.commons.enums.FeatureToggleType;
import com.hk.commons.exceptions.SpecificException;
import com.hk.web.context.MerchantContext;
import com.hk.web.context.UserContext;
import com.hk.web.controller.Routes;
import com.hk.web.controller.api.APIController;
import com.hk.web.domain.entity.Merchant;
import com.hk.web.domain.entity.MerchantFeatureToggle;
import com.hk.web.domain.entity.MerchantUserAccount;
import com.hk.web.domain.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Antinomy
 * @date 17/11/1
 */
@Api(value = "商户模块", description = "商户模块")
@RestController
public class MerchantAPIController extends APIController {

    private Logger logger = LoggerFactory.getLogger(MerchantAPIController.class);

    @Autowired
    private MerchantContext merchantContext;

    @Autowired
    private UserContext userContext;


    @ApiOperation(value = "查询华康云保险连接", notes = "查询华康云保险连接", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.API_MERCHANT_FRONTEND_HUAKANGYUN_INSURANCE, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult hkYunInsurance(HttpServletRequest request, String mobile) {

        ProcessAPI api = (jsonResult, validate) -> {
            Merchant merchant = merchantContext.getMerchantByName("华康云");

            User user = userContext.findUserByName(mobile);

            if (1 != user.getTieCard()) {
                throw new SpecificException("未绑卡不能进行保险业务,请先绑卡");
            }

            MerchantUserAccount userAccount = merchantContext.getOrCreateUserAccount(merchant, user);

            String path = "mvc/index/HCTProduct?accountId=" + userAccount.getMerchantAccountId();

            String finalUrl = merchant.getUrl() + path;
            HashMap result = new HashMap();
            result.put("url", finalUrl);

            jsonResult.result = result;
        };

        return api.process(request);
    }

    @ApiOperation(value = "查询商户特性开关", notes = "查询商户特性开关", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.API_MERCHANT_FRONTEND_APP_FEATURE_TOGGLES, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult findAllFeatureToggles(HttpServletRequest request, Long userId, FeatureToggleType type) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<MerchantFeatureToggle> allFeatureToogle;

            if (type != null) {
                allFeatureToogle = new ArrayList<>();
                MerchantFeatureToggle featureToggle = merchantContext.findMerchantFeature(type);

                if (featureToggle != null) {
                    allFeatureToogle.add(featureToggle);
                }

            } else {
                allFeatureToogle = merchantContext.findAllFeatureToogle();
            }


            List<HashMap> result = new ArrayList<>();

            for (MerchantFeatureToggle featureToggle : allFeatureToogle) {
                HashMap item = new HashMap();
                item.put("feature", featureToggle.getFeature());
                item.put("featureName", featureToggle.getFeature().getValue());
                item.put("enable", featureToggle.getEnable());
                result.add(item);
            }

            jsonResult.result = result;
        };

        return api.process(request);
    }

}
